/*
 * UncLkp.java Created on Nov 4, 2005.
 *
 * Copyright 2004 Informatica Corporation. All rights reserved.
 * INFORMATICA PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.informatica.powercenter.sdk.mapfwk.samples;

import java.util.ArrayList;
import java.util.List;

import com.informatica.powercenter.sdk.mapfwk.connection.SourceTargetType;
import com.informatica.powercenter.sdk.mapfwk.core.Field;
import com.informatica.powercenter.sdk.mapfwk.core.FieldKeyType;
import com.informatica.powercenter.sdk.mapfwk.core.FieldType;
import com.informatica.powercenter.sdk.mapfwk.core.Mapping;
import com.informatica.powercenter.sdk.mapfwk.core.OutputSet;
import com.informatica.powercenter.sdk.mapfwk.core.PortType;
import com.informatica.powercenter.sdk.mapfwk.core.RowSet;
import com.informatica.powercenter.sdk.mapfwk.core.Session;
import com.informatica.powercenter.sdk.mapfwk.core.Source;
import com.informatica.powercenter.sdk.mapfwk.core.Target;
import com.informatica.powercenter.sdk.mapfwk.core.TransformField;
import com.informatica.powercenter.sdk.mapfwk.core.TransformHelper;
import com.informatica.powercenter.sdk.mapfwk.core.TransformationDataTypes;
import com.informatica.powercenter.sdk.mapfwk.core.UnconnectedLookup;
import com.informatica.powercenter.sdk.mapfwk.core.Workflow;

/**
 * This example applies a simple expression transformation on the Employee table
 * and writes to a target
 * 
 */
public class UncLkp extends Base {
    // /////////////////////////////////////////////////////////////////////////////////////
    // Instance variables
    // /////////////////////////////////////////////////////////////////////////////////////
    protected Source employeeSrc;
    protected Target outputTarget;

    /**
     * Create sources
     */
    protected void createSources() {
        employeeSrc = this.createItemsSource();
        folder.addSource( employeeSrc );
    }

    /**
     * Create targets
     */
    protected void createTargets() {
        outputTarget = this.createRelationalTarget( "Expression_Output", SourceTargetType.Microsoft_SQL_Server );
    }

    public void createMappings() throws Exception {
        // create a mapping
        mapping = new Mapping( "UncMapping", "mapping", "Testing UncMapping sample" );
        setMapFileName( mapping );
        TransformHelper helper = new TransformHelper( mapping );
        // creating DSQ Transformation
        OutputSet outSet = helper.sourceQualifier( employeeSrc );
        RowSet dsqRS = (RowSet) outSet.getRowSets().get( 0 );
        // create an expression Transformation
        // the fields LastName and FirstName are concataneted to produce a new
        // field fullName
        String expr = "string(80, 0) firstName1= IIF(ISNULL(:LKP.lkp(ItemId, Item_Name)), "
                + "DD_UPDATE, DD_REJECT)";
        TransformField outField = new TransformField( expr );
        RowSet expRS = (RowSet) helper.expression( dsqRS, outField, "exp_transform" ).getRowSets()
                .get( 0 );
        // create a unconnected lookup transformation
        // set the return port. return port should be either from input or lookup source.
        Field retField = new Field( "Item_No1", "Item_No", "", TransformationDataTypes.INTEGER, "10",
                "0", FieldKeyType.PRIMARY_KEY, FieldType.TRANSFORM, false );
        // create input and output fields
        List<Field> input = new ArrayList<Field>();
        createUncLkpFields( input );
        // create an unconnected lookup
        String condition = "ItemId = EmployeeID AND Item_Name = LastName";
        UnconnectedLookup uncLkp = helper.unconnectedLookup( "lkp", retField, input, condition,
                employeeSrc );
        // write to target
        mapping.writeTarget( expRS, outputTarget );
        folder.addMapping( mapping );
    }

    // create the fields for unconnected lookup transformation
    void createUncLkpFields( List<Field> input ) {
        Field field1 = new Field( "EmployeeID", "EmployeeID", "", TransformationDataTypes.INTEGER, "10",
                "0", FieldKeyType.PRIMARY_KEY, FieldType.TRANSFORM, false );
        input.add( field1 );
        Field field2 = new Field( "LastName", "LastName", "", TransformationDataTypes.STRING, "20", "0",
                FieldKeyType.NOT_A_KEY, FieldType.TRANSFORM, false );
        input.add( field2 );
        Field field3 = new Field( "FirstName", "FirstName", "", TransformationDataTypes.STRING, "10",
                "0", FieldKeyType.NOT_A_KEY, FieldType.TRANSFORM, false );        
    }

    public static void main( String args[] ) {
        try {
            UncLkp unclTrans = new UncLkp();
            if (args.length > 0) {
                if (unclTrans.validateRunMode( args[0] )) {
                    unclTrans.execute();
                }
            } else {
                unclTrans.printUsage();
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println( "Exception is: " + e.getMessage() );
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.informatica.powercenter.sdk.mapfwk.samples.Base#createSession()
     */
    protected void createSession() throws Exception {
        session = new Session( "Session_For_UncMapping", "Session_For_UncMapping",
                "This is session for UncMapping" );
        session.setMapping( this.mapping );
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.informatica.powercenter.sdk.mapfwk.samples.Base#createWorkflow()
     */
    protected void createWorkflow() throws Exception {
        workflow = new Workflow( "Workflow_for_UncMapping", "Workflow_for_Expression",
                "This workflow for UncMapping" );
        workflow.addSession( session );
        folder.addWorkFlow( workflow );
    }
}
